#ifndef TESTAABB_H
#define TESTAABB_H

#include "TestScene.h"

class TestAabb:public TestScene{
public:
    void render(){
        mRenderer->render(mCamera);
    }

    void onKeyboard(unsigned char kc,int x,int y){
        vec3 t;
        vec3 r;
        float d=0;
        switch(kc){
        case 'w':
            t.y+=0.1f;
            break;
        case 's':
            t.y-=0.1f;
            break;
        case 'a':
            t.x-=0.1f;
            break;
        case 'd':
            t.x+=0.1f;
            break;
        case 'i':
            d+=1;
            r.x=1;
            break;
        case 'k':
            d-=1;
            r.x=1;
            break;
        case 'j':
            d-=1;
            r.y=1;
            break;
        case 'l':
            d+=1;
            r.y=1;
            break;
        }
        mNode->translate(t,TS_PARENT);
    }
protected:

    void setupCamera(){
        mCamera=new Camera(mScene);
        mCamera->setFar(10);
        mCamera->setPosition(vec3(0,0,4),TS_PARENT);
        mRootNode->attachChild(mCamera);
        Viewport* vp=mCamera->getViewport();
        vp->mClearColor=vec4(0.3,0.2,0.1,0);
        vp->setRect(0,0,mWidth,mHeight);
    }

    void setupDisplay(){
        ShaderPtr simple(new Shader());
        simple->fromFile("glsl/simple.vs","glsl/simple.fs");
        MaterialPtr mat(new Material(simple));
        mat->setShininess(50.0);
        mat->setColor(vec4(1,0,0,1));

        SceneUtil ss;
        GeometryMap geos;
        ss.loadGeometries("media/teapot.obj",geos);
        GeometryPtr sphere=geos.begin()->second;
        sphere->updateTangent();

        MeshEntity* ent=new MeshEntity();
        ent->setGeometry(sphere);
        ent->setMaterial(mat);
        mNode=new SceneNode();
        mNode->attachEntity(ent);
        mRootNode->attachChild(mNode);

        sphere->updateAabb();
        const Aabb& box=sphere->getAabb();
        mAabb=new AabbEntity();
        mAabb->setAabb(box);
        mAabb->setColor(vec4(1,0,0,1));
        mNode->attachEntity(mAabb);
    }
protected:
    Camera* mCamera;
    SceneNode* mNode;
    AabbEntity* mAabb;
};

#endif